package cn.fuzongyao.learning.android.crime08;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.time.LocalDate;
import java.time.format.TextStyle;
import java.util.List;
import java.util.Locale;

public class CrimeListFragment extends Fragment {
    private static final String TAG = "CriminalIntent";
    private RecyclerView recyclerView;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_crime_list, container, false);
        recyclerView = view.findViewById(R.id.crime_recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        updateUI();
        return view;
    }

    private void updateUI() {
        CrimeLab crimeLab = CrimeLab.getInstance();
        List<Crime> crimes = crimeLab.getCrimes();
        recyclerView.setAdapter(new CrimeAdapter(crimes));
    }

    private class CrimeHolder extends RecyclerView.ViewHolder {
        private Crime crime;
        private TextView titleTextView;
        private TextView dateTextView;

        public CrimeHolder(LayoutInflater inflater, ViewGroup parent) {
            super(inflater.inflate(R.layout.list_item_crime, parent, false));
            Log.d(TAG, "CrimeHolder: ");

            titleTextView = itemView.findViewById(R.id.crime_list_title);
            dateTextView = itemView.findViewById(R.id.crime_list_date);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(getActivity(), crime.getTitle() + " clicked!", Toast.LENGTH_SHORT).show();
                }
            });
        }


        public void bind(Crime crime) {
            Log.d(TAG, "bind: " + crime);
            this.crime = crime;
            titleTextView.setText(crime.getTitle());
            LocalDate date = crime.getDate();
            String week = date.getDayOfWeek().getDisplayName(TextStyle.SHORT, Locale.getDefault());
            String dateString = week + " " + date.toString();
            dateTextView.setText(dateString);
        }
    }

    private class CrimeAdapter extends RecyclerView.Adapter<CrimeHolder> {
        private List<Crime> crimes;

        public CrimeAdapter(List<Crime> crimes) {
            Log.d(TAG, "CrimeAdapter: ");
            this.crimes = crimes;
        }

        @NonNull
        @Override
        public CrimeHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            Log.d(TAG, "onCreateViewHolder: ");
            return new CrimeHolder(LayoutInflater.from(getActivity()), parent);
        }

        @Override
        public void onBindViewHolder(@NonNull CrimeHolder holder, int position) {
            Log.d(TAG, "onBindViewHolder: ");
            holder.bind(crimes.get(position));
        }

        @Override
        public int getItemCount() {
            Log.d(TAG, "getItemCount: ");
            return crimes.size();
        }
    }
}
